package com.hncc.sms.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.hncc.sms.domain.SysRoleManager;
import com.hncc.sms.service.ISysRoleManagerService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hncc.sms.domain.SysUserManager;
import com.hncc.sms.service.ISysUserManagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;

/**
 * 用户信息Controller
 *
 * @author ruoyi
 * @date 2020-08-10
 */
@RestController
@RequestMapping("/sms/user")
@Api(tags = "用户信息接口")
public class SysUserManagerController extends BaseController
{
    @Resource
    private ISysUserManagerService sysUserManagerService;

    @Resource
    private ISysRoleManagerService sysRoleManagerService;

    /**
     * 查询用户信息列表
     */
    @ApiOperation(value = "条件分页查询用户信息",httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('sms:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserManager sysUserManager)
    {
        startPage();
        List<SysUserManager> list = sysUserManagerService.selectSysUserManagerList(sysUserManager);
        return getDataTable(list);
    }

    /**
     * 查询所有参与排班的医生
     * @return
     */
    @GetMapping("/selectSysUserManagerBySkdFlag")
    public TableDataInfo selectSysUserManagerBySkdFlag(SysUserManager sysUserManager){
        startPage();
        List<SysUserManager> list = sysUserManagerService.selectSysUserManagerByDeptId(sysUserManager.getDeptId());
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('sms:user:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysUserManager sysUserManager)
    {
        List<SysUserManager> list = sysUserManagerService.selectSysUserManagerList(sysUserManager);
        ExcelUtil<SysUserManager> util = new ExcelUtil<SysUserManager>(SysUserManager.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户信息详细信息
     */
    @ApiOperation(value = "根据userId获取用户信息详细信息",httpMethod = "GET",
            notes = "根据userId查询用户信息")
    @ApiImplicitParam(paramType = "path",name = "userId",value = "用户信息id",dataType = "Long",required = true)
    @PreAuthorize("@ss.hasPermi('sms:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId))
        {
            ajax.put(AjaxResult.DATA_TAG, sysUserManagerService.selectSysUserManagerById(userId));
            ajax.put("roleIds", sysRoleManagerService.selectSysRoleManagerListBySysUserManagerId(userId));
        }
        return ajax;
    }

    /**
     * 新增用户信息
     */
    @ApiOperation(value = "新增",httpMethod = "POST", notes = "新增用户信息")
    @ApiImplicitParam(name = "sysUserManager", value = "新增用户信息", dataType = "SysUserManager")
    @PreAuthorize("@ss.hasPermi('sms:user:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUserManager sysUserManager)
    {
        if(UserConstants.NOT_UNIQUE.equals(sysUserManagerService.checkUserNameUnique(sysUserManager.getUserName()))){
            return AjaxResult.error("新增用户'" + sysUserManager.getUserName() + "'失败，用户名已存在");
        }
        sysUserManager.setCreateBy(SecurityUtils.getUsername());
        //密码加密
        sysUserManager.setPassword(SecurityUtils.encryptPassword(sysUserManager.getPassword()));
        return toAjax(sysUserManagerService.insertSysUserManager(sysUserManager));
    }

    /**
     * 修改用户信息
     */
    @ApiOperation(value = "修改", httpMethod = "PUT", notes = "修改用户信息")
    @ApiImplicitParam(name = "sysUserManager", value = "修改用户信息", dataType = "SysUserManager")
    @PreAuthorize("@ss.hasPermi('sms:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserManager sysUserManager)
    {
        sysUserManager.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysUserManagerService.updateSysUserManager(sysUserManager));
    }

    /**
     * 删除用户信息
     */
    @ApiOperation(value = "批量删除", httpMethod = "DELETE", notes = "根据userIds删除用户信息")
    @ApiImplicitParam(paramType = "path",name = "userIds",value = "用户信息id",dataType = "Long[]",required = true)
    @PreAuthorize("@ss.hasPermi('sms:user:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userId}")
    public AjaxResult remove(@PathVariable Long userId)
    {
        return toAjax(sysUserManagerService.deleteSysUserManagerById(userId));
    }

    /**************************************/


}
